<?php
session_start();
// 数据库配置
$host = 'localhost';
$dbname = 'itops_help1_center';
$username = 'itops_help1_center';
$password = 'itops_help1_center';

$article = null;
$error = '';

if (isset($_GET['id']) && is_numeric($_GET['id'])) {
    $id = (int)$_GET['id'];
    try {
        $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        // 查询文章详情
        $stmt = $pdo->prepare("SELECT a.*, c.name as category_name 
                            FROM article a 
                            LEFT JOIN article_category c ON a.category_id = c.id 
                            WHERE a.id = :id");
        $stmt->bindParam(':id', $id);
        $stmt->execute();
        $article = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$article) {
            $error = "文章不存在或已被删除";
        } else {
            // 增加阅读量
            $pdo->prepare("UPDATE article SET read_count = read_count + 1 WHERE id = :id")
                ->execute([':id' => $id]);
        }
    } catch (PDOException $e) {
        $error = "数据加载失败：" . $e->getMessage();
    }
} else {
    $error = "无效的文章ID";
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $article ? htmlspecialchars($article['title']) : '文章详情 - IT技术知识库'; ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;500&family=Inter:wght@400;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="../css/common.css">
    <link rel="stylesheet" href="css/main.css">
</head>
<body>
    <!-- 导航栏 -->
    <header class="site-header">
        <div class="container">
            <div class="header-wrapper">
                <a href="../index.php" class="header-logo">
                    <span class="logo-icon">📚</span>
                    <span class="logo-text">IT技术知识库</span>
                </a>

                <ul class="nav-list">
                    <li><a href="../index.php" class="nav-link">首页</a></li>
                    <li><a href="versions.php" class="nav-link">版本记录</a></li>
                    <li><a href="articles.php" class="nav-link">文章列表</a></li>
                    <li><a href="search.php" class="nav-link">搜索</a></li>
                    <?php if (isset($_SESSION['username'])): ?>
                        <li><span class="nav-user"><?php echo htmlspecialchars($_SESSION['username']); ?></span></li>
                        <li><a href="../admin/logout.php" class="nav-link">退出</a></li>
                    <?php else: ?>
                        <li><a href="../admin/login.php" class="nav-link">登录</a></li>
                    <?php endif; ?>
                </ul>
            </div>
        </div>
    </header>

    <!-- 主内容区 -->
    <div class="container">
        <?php if ($error): ?>
            <div class="error-hint"><?php echo $error; ?></div>
            <a href="articles.php" class="btn btn-outline mt-2">返回文章列表</a>
        <?php else: ?>
            <article class="article-detail card">
                <div class="detail-header">
                    <h1 class="detail-title"><?php echo htmlspecialchars($article['title']); ?></h1>
                    <div class="detail-meta">
                        <span>分类：<?php echo htmlspecialchars($article['category_name'] ?? '未分类'); ?></span>
                        <span>发布时间：<?php echo $article['create_time']; ?></span>
                        <span>阅读量：<?php echo $article['read_count'] + 1; ?></span>
                    </div>
                </div>
                
                <div class="detail-content">
                    <?php 
                    // 代码块处理（IT风格）
                    $content = $article['content'];
                    $content = preg_replace('/```(.*?)```/s', '<code class="code-block">$1</code>', $content);
                    echo nl2br(htmlspecialchars($content, ENT_QUOTES, 'UTF-8'));
                    ?>
                </div>
            </article>

            <div class="mt-3">
                <a href="articles.php" class="btn btn-outline">返回文章列表</a>
                <?php if (isset($_SESSION['is_admin']) && $_SESSION['is_admin'] == 1): ?>
                    <a href="../admin/article_edit.php?id=<?php echo $article['id']; ?>" class="btn btn-primary">编辑文章</a>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
</body>
</html>